package com.example.Controller;

import com.example.Service.UserService;
import com.example.pojo.Result;
import com.example.pojo.User;
import com.example.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.validation.constraints.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

//用户注册登录(完结)
@Validated
@RestController
@RequestMapping("/login")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    //这里的邮箱废弃，暂时不做修改，技术有限，无法实现
    //用户注册
    @PostMapping("/signUp")
    public Result singUp(@Pattern(regexp = "^1[3456789]\\d{9}$") String phone,
                         @Pattern(regexp = "^[0-9a-zA-Z_-]+@[0-9a-zA-Z_-]+\\.(com|cn|net|vip|cloud|aliyun|163|sina)$") String email,
                         @Pattern(regexp = "^(?=.*\\d)(?=.*[a-zA-Z])(?=.*[^\\da-zA-Z\\s]).{6,15}$") String password,
                         @Pattern(regexp = "^(?=.*\\d)(?=.*[a-zA-Z])(?=.*[^\\da-zA-Z\\s]).{6,15}$") String enterPassword){

        log.info("手机号{}邮箱{}密码{}确认密码{}",phone,email,password,enterPassword);
        if(!password.equals(enterPassword)){
            System.out.println(password != enterPassword);
            return Result.error("两次输入密码不一致");
        }

        //1.查看用户是否存在
        User e = userService.selectUser(phone,email);

        if(e != null){
            return Result.error("该用户已被注册");
        }

        //没有被注册过，就进行添加
        userService.addUser(phone,email,password);
        return Result.success();
    }

    //用户登录
    @PostMapping
    public Result login(@Pattern(regexp = "^1[3456789]\\d{9}$") String phone,
                        @Pattern(regexp = "^(?=.*\\d)(?=.*[a-zA-Z])(?=.*[^\\da-zA-Z\\s]).{6,15}$") String password,
                        @Pattern(regexp = "^[0-9a-zA-Z_-]+@[0-9a-zA-Z_-]+\\.(com|cn|net|vip|cloud|aliyun|163|sina)$") String email){
        //检查该用户是否存在
        User e = userService.selectUser(phone,email);

        if(e == null){
            return Result.error("改用户不存在，请先注册");
        }

        //用户存在进行密码的校验
        if(password.equals(e.getPassword())){

            //密码通过生成jwt令牌
            Map<String,Object> map = new HashMap<>();
            map.put("id",e.getId());
            map.put("phone",e.getPhoneNumber());

            //生成jwt
            String jwt = JwtUtils.generateJwt(map);
            log.info("不为空的e3{}",e);
            return Result.success(jwt);

        }
        log.info("不是密码错误的e{}",e);

        return Result.error("密码错误");
    }

    //获取用户登录的基本信息
    @GetMapping
    public Result lookUserInfo(@RequestHeader(name = "token") String token){

        Claims claims = JwtUtils.parseJWT(token);
        String phone = (String) claims.get("phone");

        //这里返回的用户详情不包含密码
        User e = userService.selectUser1(phone);

        return Result.success(e);
    }

    //修改用户密码
    @PostMapping("/repd")
    public Result updataPassWd( @Pattern(regexp = "^(?=.*\\d)(?=.*[a-zA-Z])(?=.*[^\\da-zA-Z\\s]).{6,15}$") String oldPassword,
                                @Pattern(regexp = "^(?=.*\\d)(?=.*[a-zA-Z])(?=.*[^\\da-zA-Z\\s]).{6,15}$") String newPassword,
                                @Pattern(regexp = "^(?=.*\\d)(?=.*[a-zA-Z])(?=.*[^\\da-zA-Z\\s]).{6,15}$") String enterPassword,
                                @RequestHeader(name = "token") String token){
//        1.获取当前用户id
        Claims claims = JwtUtils.parseJWT(token);
        String phone = (String) claims.get("phone");
        User e = userService.selectUser(phone,null);
        int createId = e.getId();

        log.info("旧密码{}新密码{}当前用户{}",oldPassword,newPassword,createId);
        //1.判断原密码和确认密码是否相同
        if(!newPassword.equals(enterPassword)){
            return Result.error("两次密码输入不一致");
        }

        //2.获取原密码账号
        User user01 = userService.selectUser02(oldPassword,createId);
        log.info("查询账户{}",user01);
        System.out.println(user01);

        //为空密码错误
        if(user01 == null){
            return Result.error("原密码输入错误");
        }

        //不为空修改密码
        userService.updataPasswd(newPassword,createId);

        return Result.success("修改成功");

    }
}
